package com.work.saturn.account;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.coorchice.library.SuperTextView;
import com.immo.libcommon.base.BaseActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.utils.CountTimeUtil;
import com.immo.libcommon.utils.InputValidationUtils;
import com.immo.libcommon.utils.MD5Util;
import com.work.saturn.R;
import com.work.saturn.bean.CheckSMSBean;
import com.work.saturn.bean.GetCodeBean;
import com.work.saturn.bean.RegisterBean;
import com.immo.libcommon.constant.RouterUtil;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.immo.libcommon.utils.InputValidationUtils.inputIsEmpty;
import static com.immo.libcommon.utils.InputValidationUtils.inputIsRule;

/**
 * @author Administrator
 * @content 注册页面
 * @date 2018/7/10
 */
@Route(path = RouterUtil.REGISTER_PAGE)
public class RegisterActivity extends BaseActivity {
    /**
     * 用户名
     */
    @BindView(R.id.register_input_username)
    EditText username;
    /**
     * 验证码
     */
    @BindView(R.id.register_input_code)
    EditText inputCode;
    /**
     * 获取验证码
     */
    @BindView(R.id.register_input_get_code)
    SuperTextView getCode;
    /**
     * 密码
     */
    @BindView(R.id.register_input_password)
    EditText password;
    /**
     * 手机号
     */
    @BindView(R.id.register_input_phone)
    EditText phoneEt;
    /**
     * 定时器
     */
    private CountTimeUtil time;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //禁止进入时弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        ButterKnife.bind(this);
        //初始化定时器
        time = new CountTimeUtil(60000, 1000, getCode);
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        return inputIsEmpty(this, username.getText().toString(), getString(R.string.username_no_empty))
                && inputIsEmpty(this, phoneEt.getText().toString(), getString(R.string.phone_no_empty))
                && inputIsRule(this, phoneEt.getText().toString(), getString(R.string.phone_rule_error), InputValidationUtils.Phone)
                && inputIsEmpty(this, password.getText().toString(), getString(R.string.password_no_empty))
                && inputIsEmpty(this, inputCode.getText().toString(), getString(R.string.code_no_empty));
    }

    /**
     * 获取验证码
     */
    private void getSMSCode() {
        new NetworkRequest((object, data) -> {
            if (object instanceof GetCodeBean) {
                //获取验证码成功后启动定时器
                time.start();
                GetCodeBean getCodeBean = (GetCodeBean) object;
                toast(getCodeBean.getData());
            }
        }).requestGet(BaseUrl.GETSMSCODE + "?phone=" + phoneEt.getText().toString(),
                this, GetCodeBean.class, false, false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在页面离开的时候定时器的要结束
        time.onFinish();
    }

    /**
     * 校验验证码
     */
    private void checkSMSCode() {
        HashMap<String, String> params = new HashMap<>();
        params.put("phone", phoneEt.getText().toString());
        params.put("code", inputCode.getText().toString());
        new NetworkRequest((object, data) -> {
            if (object instanceof CheckSMSBean) {
                CheckSMSBean checkSMSBean = (CheckSMSBean) object;
                if (checkSMSBean.getCode()==0) {
                    registerNet();
                } else {
                    toast("验证码错误");
                }
            }
        }).requestPostKey(BaseUrl.CHECKSMSCODE,
                this, params, CheckSMSBean.class, false, false);
    }

    /**
     * 注册
     */
    private void registerNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("username", username.getText().toString());
        params.put("role", "user");
        params.put("email", "");
        params.put("phone", phoneEt.getText().toString());
        params.put("password", MD5Util.md5Password(password.getText().toString()));
        new NetworkRequest((object, data) -> {
            if (object instanceof RegisterBean) {
                RegisterBean registerBean = (RegisterBean) object;
                if (registerBean.isSuccess()) {
                    toast("注册成功");
                    finish();
                }
            }
        }).requestPost(BaseUrl.REGISTER,
                this, JSON.toJSONString(params), RegisterBean.class, true, false);
    }

    @OnClick({R.id.register_back, R.id.register_input_get_code, R.id.register_submit, R.id.register_to_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //返回
            case R.id.register_back:
                finish();
                break;
            case R.id.register_input_get_code:
                //获取验证码
                String phone = phoneEt.getText().toString();
                if (!phone.isEmpty() && phone.matches(InputValidationUtils.Phone)) {
                    getSMSCode();
                } else {
                    toast(getString(R.string.phone_rule_error));
                }
                break;
            case R.id.register_submit:
                //提交
                if (isCheck()) {
                    checkSMSCode();
                }
                break;
            case R.id.register_to_register:
                //跳到登录
                finish();
                break;
        }
    }

}
